﻿using System;

namespace ICommonLibrary.Collection {

    public class Bits {

        // Container.
	    private int i = 0;

        /// <summary>
        /// Constructs an instance with all bits initialized to 0.
        /// </summary>
	    public Bits() {
	    }

        /// <summary>
        /// Constructs an instance and initialisation.
        /// </summary>
        /// <param name="init">initial value.</param>
	    public Bits(int init) {
		    i = init;
	    }
    	
        /// <summary>
        /// Finds out the locations of "1" bits from the given integer. Returns true
        /// if every bit at the same location in the container is already set to "1".
        /// </summary>
        /// <param name="bits">an integer to test with.</param>
        /// <returns>true or false</returns>
	    public bool IsSet(int bits) {
		    return (i & bits) == bits;
	    }

        /// <summary>
        /// Finds out the locations of "1" bits from the given integer, and then make
        /// sure every bit at the same location in the container is set to "1".
        /// </summary>
        /// <param name="bits"> an integer.</param>
	    public void Set(int bits) {
		    Set(bits, true);
	    }
        
   	    /// <summary>
   	    /// Finds out the locations of "1" bits from the given integer, and then make
   	    /// sure every bit at the same location in the container is set to either "0"
   	    /// or "1", depending on the second argument. 
   	    /// </summary>
   	    /// <param name="bits">an integer.</param>
   	    /// <param name="toOne">true indicates setting those bits to 1, setting to 0 
   	    /// otherwise.</param>
	    public void Set(int bits, bool toOne) {
		    if (toOne)
			    i |= bits;
		    else
			    i &= ~bits;
	    }
    	
	    /// <summary>
        /// Returns a copy of the container.
	    /// </summary>
	    /// <returns></returns>
	    public int ToInt() {
		    return i;
	    }
    	
	    public new String ToString() {
		    return i.ToString();
	    }
    }
}
